package reduce.fz.com.reduce.fragment;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.app.Fragment;
import android.provider.BaseColumns;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.fastjson.TypeReference;

import java.util.List;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.activity.SubjectDetailActivity;
import reduce.fz.com.reduce.adapter.BaseRecycleViewHolder;
import reduce.fz.com.reduce.bean.ServiceException;
import reduce.fz.com.reduce.bean.StoryReply;
import reduce.fz.com.reduce.bean.TeamCategory;
import reduce.fz.com.reduce.bean.TeamSubject;
import reduce.fz.com.reduce.bean.page.Pagination;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.custom.delegate.RecyclerViewDelegate;
import reduce.fz.com.reduce.custom.view.RoundNetworkImageView;
import reduce.fz.com.reduce.fragment.base.BaseListFragment;
import reduce.fz.com.reduce.util.ApiUtil;
import reduce.fz.com.reduce.util.DateUtil;
import reduce.fz.com.reduce.util.HttpUtils;

/**
 * A simple {@link Fragment} subclass.
 */
public class TeamSubjectListFragment extends BaseListFragment<TeamSubjectListFragment.SubjectListViewHolder, TeamSubject> {

    private Long teamid;

    private RecyclerViewDelegate mRecyclerViewDelegate = new RecyclerViewDelegate();


    public boolean isViewBeingDragged(MotionEvent event) {
        return mRecyclerViewDelegate.isViewBeingDragged(event, recyclerView);
    }


    public static TeamSubjectListFragment create(Long teamid) {
        TeamSubjectListFragment fragment = new TeamSubjectListFragment();
        fragment.setSupportLoadingDialog(false);
        Bundle bundle = new Bundle();
        bundle.putLong("teamid", teamid);
        fragment.setArguments(bundle);
        return fragment;
    }


    public void forwardSubjectDetail(TeamSubject subject)
    {
        Intent intent=new Intent(getActivity(), SubjectDetailActivity.class);
        Bundle bundle=new Bundle();
        bundle.putSerializable("subject",subject);
        bundle.putLong("teamid", teamid);
        intent.putExtras(bundle);
        startActivity(intent);

    }

    public interface ForwardSubjectDetailListener{
        public void itemClick(TeamSubject subject);
    }

    public void addReply(TeamSubject reply)

    {
        addFrist(reply);
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        teamid = getArguments().getLong("teamid");

    }

    @Override
    public LinearLayoutManager getLayoutManager() {
        //默认返回列表
        return null;
    }

    @Override
    protected SubjectListViewHolder createItemViewHolder(ViewGroup parent) {
        View view = layoutInflater.inflate(R.layout.team_subject_item, parent, false);
        SubjectListViewHolder holder = new SubjectListViewHolder(view, new ForwardSubjectDetailListener() {
            @Override
            public void itemClick(TeamSubject subject) {
                forwardSubjectDetail(subject);
            }
        });
        return holder;
    }

    @Override
    protected SubjectListViewHolder createfootViewHolder(View view) {
        return new SubjectListViewHolder(view);
    }

    //请求URL
    @Override
    public void request(String url) {
        HttpUtils.get(url, new AbResponseListener<List<TeamSubject>>(new TypeReference<Result<List<TeamSubject>>>() {
        }) {
            @Override
            public void error(ServiceException e) {
            }

            @Override
            public void responseInfo(Result<List<TeamSubject>> data) {
                addResult(data);
            }
        });
    }

    @Override
    protected String setUrlBeforeRequest(List<TeamSubject> datas, Pagination pagination) {
        String url = ApiUtil.getApi(context, R.array.api_team_subject_list, teamid, pagination.getPageNo(), pagination.getPageSize());
        return url;
    }

    @Override
    protected int getPageType() {
        return Pagination.TYPE_START_NUM;
    }

    public static class SubjectListViewHolder extends BaseRecycleViewHolder<TeamSubject> {
        private RoundNetworkImageView mUserHeader;
        private TextView mTvNickName;
        private TextView pubTime;
        private TextView mDesc;
        private View itemView;
        private ForwardSubjectDetailListener listener;
        public SubjectListViewHolder(View itemView,ForwardSubjectDetailListener listener)
        {
            this(itemView);
            this.listener=listener;
        }


        public SubjectListViewHolder(View itemView) {
            super(itemView);
            this.itemView=itemView;
            mUserHeader = (RoundNetworkImageView) itemView.findViewById(R.id.rnv_user_header);
            mTvNickName = (TextView) itemView.findViewById(R.id.tv_user_nickname);
            pubTime = (TextView) itemView.findViewById(R.id.tv_reply_time);
            mDesc = (TextView) itemView.findViewById(R.id.tv_reply_content);
        }

        @Override
        public void renderView(final TeamSubject data, int position) {
            if (data.user == null) return;
            mUserHeader.setImageUrl(data.user.avatar, HttpUtils.getAvatarImageLoader());
            mTvNickName.setText(data.user.nickname);
            mDesc.setText(data.description);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.itemClick(data);
                }
            });

            pubTime.setText(DateUtil.getInterval(data.pubtime));
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }
}
